home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / MathLink / MLExample_1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-19  |  4.0 KB  |  203 lines

  1. #import <stdio.h>
  2. #import "/LocalApps/Mathematica.app/Library/Mathematica/MathLink/Includes/mathlink.h"
  3.  
  4. #define DEBUG FALSE
  5.  
  6. void  error(MLINK);
  7. void  read_and_print_expression(MLINK);
  8. int   ReadPacket(MLINK);
  9.  
  10. void example_1(char *,double *,int *,double,double,double);
  11.  
  12. MLINK link;
  13.  
  14. main()
  15. {
  16.     int i,n;
  17.     double min,max,del,result[1000];
  18.  
  19.     link = MLStart("math -mathlink -batchoutput -noinit");
  20.  
  21.     min = 0.0;
  22.     max = 1.0;
  23.     del = .5;
  24.     
  25.     example_1("x y",result,&n,min,max,del);
  26.     for (i=0 ; i < n ; i++)
  27.        printf("%d , %f\n",i,result[i]);       
  28.  
  29.     MLClose(link);
  30. }
  31.  
  32. void example_1(function,realarray,num,min,max,del)
  33. char *function;
  34. double *realarray,min,max,del;
  35. int *num;
  36. {    
  37.     int i;
  38.     double rtnreal;
  39.     
  40.     MLPutFunction(link,"Chop",1);
  41.       MLPutFunction(link,"SetPrecision",2);
  42.         MLPutFunction(link,"Flatten",1);
  43.           MLPutFunction(link,"Table",3);
  44.             MLPutFunction(link,"List",1);
  45.               MLPutFunction(link,"ToExpression",1);
  46.             MLPutString(link, function);
  47.             MLPutFunction(link,"List",4);
  48.               MLPutSymbol(link,"x");
  49.           MLPutReal(link,min);
  50.           MLPutReal(link,max);
  51.           MLPutReal(link,del);
  52.             MLPutFunction(link,"List",4);
  53.               MLPutSymbol(link,"y");
  54.           MLPutReal(link,min);
  55.           MLPutReal(link,max);
  56.           MLPutReal(link,del);
  57.         MLPutInteger(link,3);
  58.     MLEndPacket(link);
  59.  
  60.     if (DEBUG)
  61.     {
  62.       read_and_print_expression(link);
  63.       return;
  64.     }    
  65.  
  66.     while (MLNextPacket(link) != RETURNPKT)
  67.        MLNewPacket(link);
  68.  
  69.     MLCheckFunction(link,"List",num);
  70.     for (i=0 ; i < *num ; i++)
  71.     {
  72.        MLGetReal(link,&rtnreal);
  73.        realarray[i] = rtnreal;       
  74.     }
  75. }  
  76.  
  77. void  error( mlp) MLINK mlp;
  78. {
  79.     fprintf( stderr, "\nerror: %s\n", MLErrorMessage( mlp));
  80.     MLClose( mlp);
  81.     exit( 0);
  82. }  /* error */
  83.  
  84. void  read_and_print_expression( p) MLINK p;
  85. {
  86.     char  *s;
  87.     int    n, i, len;
  88.     double r;
  89.     static int indent;
  90.     
  91.     i=MLGetNext(p);
  92.     fprintf(stderr, "Packet type = %d\n",i);
  93.     
  94.     switch (i) {    
  95.     case MLTKSYM:
  96.         MLGetSymbol( p, &s);
  97.         fprintf( stderr, "%s ", s);
  98.         break;
  99.     case MLTKSTR:
  100.         MLGetString( p, &s);
  101.         fprintf( stderr, "\"%s\" ", s);
  102.         break;
  103.     case MLTKINT:
  104.         MLGetInteger( p, &n);
  105.         fprintf( stderr, "%d ", n);
  106.         break;
  107.     case MLTKREAL:
  108.         MLGetReal( p, &r);
  109.         fprintf( stderr, "%f ", r);
  110.         break;
  111.     case MLTKFUNC:
  112.         indent += 3;
  113.         fprintf( stderr, "\n %*.*s", indent, indent, "");
  114.         if (MLGetArgCount( p, &len) == 0) {
  115.             error( p);
  116.         }else{
  117.             read_and_print_expression(p);
  118.             fprintf( stderr, "[");
  119.             for (i = 1; i <= len; ++i) {
  120.                 read_and_print_expression(p);
  121.                 if (i != len) fprintf( stderr, ", ");
  122.             }
  123.             fprintf( stderr, "]");
  124.         }
  125.         indent -= 3;
  126.         break;
  127.     case MLTKERROR:
  128.     default:
  129.         error( p);
  130.     }
  131. }  /* read_and_print_expression */
  132.  
  133.  
  134. int   ReadPacket( mlp)  MLINK mlp;
  135. {    int code;
  136.     int len = 1;
  137.  
  138.     switch (code = MLNextPacket(mlp)) {
  139.     case INPUTPKT:
  140.         fprintf( stderr, "<INPUTPKT>");
  141.         break;
  142.     case OUTPUTPKT:
  143.         fprintf( stderr, "<OUTPUTPKT>");
  144.         break;
  145.     case TEXTPKT:
  146.         if (DEBUG) fprintf( stderr, "<TEXTPKT>");
  147.         break;
  148.     case RETURNPKT:
  149.         fprintf( stderr, "Result (expression):");
  150.         break;
  151.     case RETURNTEXTPKT:
  152.         fprintf( stderr, "=");
  153.         break;
  154.     case MESSAGEPKT:
  155.         len = 2;
  156.         fprintf( stderr, "\nMessage packet:");
  157.         break;
  158.     case CALLPKT:
  159.         fprintf( stderr, "<CALLPKT>");
  160.         break;
  161.     case INPUTNAMEPKT:
  162.         fprintf( stderr, "\n");
  163.         break;
  164.     case OUTPUTNAMEPKT:
  165.         if (DEBUG) fprintf( stderr, "<OUTPUTNAMEPKT>");
  166.         break;   
  167.     case CONTROLPKT:
  168.         fprintf( stderr, "<CONTROLPKT>");
  169.         break;   
  170.     case SYNTAXPKT:
  171.         fprintf( stderr, "<SYNTAXERROR>");
  172.         break;   
  173.     default:
  174.         fprintf( stderr, "unknown packet \"%d\"\n", code);
  175.         return 0;
  176.     }
  177.     while (len --) {
  178.         read_and_print_expression( mlp);
  179.     }
  180.     if ((code != INPUTNAMEPKT) && (code != OUTPUTNAMEPKT)) {
  181.         fprintf( stderr, "\n");
  182.     }
  183.     return code;
  184. }  /* ReadPacket */
  185.  
  186. /*******************************************/
  187. /* Search for string t in string s         */
  188. /* Return position if found, otherwise -1  */
  189. /*******************************************/
  190. int instr(s,t)
  191. char *s,*t;
  192. {
  193.      int i,j,k;
  194.      
  195.      for (i=0 ; s[i] && t[0] ; i++)
  196.      {
  197.          for (j=i , k=0 ; t[k] && s[j] == t[k] ; j++ , k++);
  198.         if (!t[k]) 
  199.            return(i);
  200.      }
  201.      return(-1);
  202. }
  203.